﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScrumTime.Models;

namespace ScrumTime.Services
{
    public class UtilServices {

        public static void CreateUsabilityStories
            (int usabilityFeatureId,
             ScrumTime.Models.Story story,
             ScrumTimeEntities scrumTimeEntities        
            ) 
        {         
            //here we create the usability stories

            //primero buscamos todas las usabilits stories, q estan ligadas a esta usability features
            List<ScrumTime.Models.UsabilityStory> ubss = getUsabilityStories(usabilityFeatureId, scrumTimeEntities);
            //ahora, con estas ubs, creamos las nuevas historias de usuario
            foreach (ScrumTime.Models.UsabilityStory ubs in ubss) {
                CreateUsabilityStory(ubs, story, scrumTimeEntities);
            }
            //scrumTimeEntities.SaveChanges();
        }

        public static String ReplaceQuote(String cadena) {
            if (cadena == null)
                return cadena;
            return cadena.Replace("'", "\"");
        }


        #region "metodos privados auxiliares"

        private static List<ScrumTime.Models.UsabilityStory> getUsabilityStories(
            int usabilityFeatureId,
            ScrumTimeEntities scrumTimeEntities
            )
        {
            var results = from s in scrumTimeEntities.UsabilityStories
                          where s.UsabilityFeatureId == usabilityFeatureId                            
                          select s;
            // verificamos el resultado

            return results.ToList<ScrumTime.Models.UsabilityStory>();

        }

        private static void CreateUsabilityStory(
            ScrumTime.Models.UsabilityStory ubs,
            ScrumTime.Models.Story story,            
            ScrumTimeEntities scrumTimeEntities
            )
        {
            if (!existUsabilityStory(ubs.UsabilityStoryId, story.ProductId, scrumTimeEntities))
            {
                ScrumTime.Models.Story newStory = createUsabilityStory(ubs, story, scrumTimeEntities);
                createAcceptanceCriterias(ubs, newStory, scrumTimeEntities);
                createTasks(ubs, newStory, scrumTimeEntities);
            }
            else { 
                // verficamos si esta historia de usuario esta asociada a la usability story
                if (!isAsoctiateToUsabilityStory(story, ubs, scrumTimeEntities))
                {
                    // en caso q no este, buscamos la usabilityStory y 
                    ScrumTime.Models.Story usabilityStory = getUsabilityStory(ubs, story.ProductId,scrumTimeEntities);
                    // simplemente la asociamos
                    story.Stories1.Add(usabilityStory);                    
                }                
            }
        }

        private static ScrumTime.Models.Story getUsabilityStory(
            ScrumTime.Models.UsabilityStory ubs,
            int productId,
            ScrumTimeEntities scrumTimeEntities
        ){
            ScrumTime.Models.Story story=null;

            var results = from s in scrumTimeEntities.Stories
                          where s.UsabilityStoryId == ubs.UsabilityStoryId
                            && s.ProductId == productId
                          select s;
            story = results.First<Story>();
            return story;
        }

        private static bool isAsoctiateToUsabilityStory(
            ScrumTime.Models.Story story,
            ScrumTime.Models.UsabilityStory ubs,
            ScrumTimeEntities scrumTimeEntities
            ) 
        {            
            foreach (ScrumTime.Models.Story s_ubs in story.Stories1) {
                if (s_ubs.UsabilityStoryId == ubs.UsabilityStoryId) {
                    return true;        
                }
            }
            return false;
        }

        private static ScrumTime.Models.Story createUsabilityStory(
            ScrumTime.Models.UsabilityStory ubs,
            ScrumTime.Models.Story story,
            ScrumTimeEntities scrumTimeEntities
            ) {

                ScrumTime.Models.Story newStory =new ScrumTime.Models.Story();
                newStory.Points = 0;
                newStory.Priority = 0;
                newStory.ProductId = story.ProductId;
                newStory.UsabilityStoryId = ubs.UsabilityStoryId;
                
                // pasamos algunos datos del ubs
                newStory.UserDefinedId = ubs.Name;
                newStory.Narrative = ubs.Description;
                // por ultimo, añadimos la nueva historia de usuario.
                scrumTimeEntities.AddToStories(newStory);
                story.Stories1.Add(newStory);

                return newStory;
        }

        private static void createAcceptanceCriterias(
            ScrumTime.Models.UsabilityStory ubs,
            ScrumTime.Models.Story newStory,
            ScrumTimeEntities scrumTimeEntities    
        ) {

            foreach (ScrumTime.Models.UbS_AcceptanceCriteria ubs_ac in ubs.UbS_AcceptanceCriteria) {
                AcceptanceCriteria newAc = new AcceptanceCriteria();
                newAc.Description = ubs_ac.Description;
                newAc.Story = newStory;
                scrumTimeEntities.AddToAcceptanceCriterias(newAc);
            }
        }

        private static void createTasks(
            ScrumTime.Models.UsabilityStory ubs,
            ScrumTime.Models.Story newStory,
            ScrumTimeEntities scrumTimeEntities 
        )
        {
            foreach (ScrumTime.Models.UbS_Task ubs_task in ubs.UbS_Task)
            {
                Task newTask = new Task();
                newTask.Description = ubs_task.Description;
                newTask.Hours = ubs_task.Hours;
                newTask.Story = newStory;
                scrumTimeEntities.AddToTasks(newTask);
            }
        }

        private static bool existUsabilityStory(
            int usabilityStoryId,
            int productId,
            ScrumTimeEntities scrumTimeEntities
        )
        {
            // buscamos a todas las historias de usuario, que pertenescan a un
            // usabilityStory, y ademas que sean de este proyecto
            var results = from s in scrumTimeEntities.Stories
                          where s.UsabilityStoryId == usabilityStoryId
                            && s.ProductId == productId
                          select s;
            // verificamos el resultado
            return results.Count() > 0;
        }

        #endregion
        
    }
}



